﻿using System;
using System.Net.Http;
using Newtonsoft.Json;
using System.Text;
using System.Net.Http.Headers;
using System.Threading;

namespace _12_AI
{
    class Coze
    {
        public static string answer;
        public static async void Coze_API(string question)//发起对话(流式 stream = true,)
        {
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer pat_20D4Uu1hM5b5lYFijNp3vORK3gG7nIP7f7k1cVnSheMVSbR3oUtAfZAf8jveXZyM");
            var requestData = new
            {
                auto_save_history = true,
                stream = true,
                user_id = "me",
                bot_id = "7489376497380343820",
                additional_messages = new[]
                {
                    new{
                            role= "user" ,
                            content="你是谁"
                        }
                    }
            };//请求参数

            var jsonContent = JsonConvert.SerializeObject(requestData);//转换成json格式
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
            var response = await client.PostAsync($"https://api.coze.cn/v3/chat?", content);
            Console.WriteLine(content);
            Console.WriteLine(jsonContent);
            dynamic responseContet = await response.Content.ReadAsStringAsync();

            dynamic result = JsonConvert.DeserializeObject(responseContet);

            string str_chat_id = result.data.id;
            string str_conversation_id = result.data.conversation_id; ;

            Coze2(str_chat_id, str_conversation_id);

        }
        public static async void Coze1(string question)//发起对话(非流式 stream = false,)
        {
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer pat_20D4Uu1hM5b5lYFijNp3vORK3gG7nIP7f7k1cVnSheMVSbR3oUtAfZAf8jveXZyM");
            var requestData = new
            {
                auto_save_history = true,
                stream = false,
                user_id = "me",
                bot_id = "7489376497380343820",
                additional_messages = new[]
                {
                    new{
                            role= "user" ,
                            content=question
                        }
                    }
            };//请求参数

            var jsonContent = JsonConvert.SerializeObject(requestData);//转换成json格式
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
            var response = await client.PostAsync($"https://api.coze.cn/v3/chat?", content);
            dynamic responseContet = await response.Content.ReadAsStringAsync();

            dynamic result = JsonConvert.DeserializeObject(responseContet);

            string str_chat_id = result.data.id;
            string str_conversation_id = result.data.conversation_id; ;

            Coze2(str_chat_id, str_conversation_id);

        }
        private static async void Coze2(string str_chat_id, string str_conversation_id)//查看对话详情
        {
            //等待3秒
            Thread.Sleep(3000); // 延迟1秒

            HttpClient client = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://api.coze.cn/v3/chat/retrieve?conversation_id={str_conversation_id}&chat_id={str_chat_id}&");

            request.Headers.Add("Authorization", "Bearer pat_20D4Uu1hM5b5lYFijNp3vORK3gG7nIP7f7k1cVnSheMVSbR3oUtAfZAf8jveXZyM");

            request.Content = new StringContent("");
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            HttpResponseMessage response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            dynamic result = JsonConvert.DeserializeObject(responseBody);
            string status = result.data.status;
            if (status != "completed")
            {
                Coze2(str_chat_id, str_conversation_id);
            }
            else
            {
                Coze3(str_chat_id, str_conversation_id);
            }
        }
        private static async void Coze3(string str_chat_id, string str_conversation_id)//查看对话消息详情
        {
            HttpClient client = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://api.coze.cn/v3/chat/message/list?conversation_id={str_conversation_id}&chat_id={str_chat_id}&");

            request.Headers.Add("Authorization", "Bearer pat_20D4Uu1hM5b5lYFijNp3vORK3gG7nIP7f7k1cVnSheMVSbR3oUtAfZAf8jveXZyM");

            request.Content = new StringContent("");
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            HttpResponseMessage response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            dynamic result = JsonConvert.DeserializeObject(responseBody);

            answer = result.data[1].content; 
        }

    }
}
